package student.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import student.aistar.model.bo.Result;
import student.aistar.model.entity.Navclass;
import student.aistar.model.entity.User;
import student.aistar.model.entity.WebSeckill;
import student.aistar.service.IIndexService;
import student.aistar.service.IWatchSeckillService;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class WatchSeckillController {

    @Autowired
    private IWatchSeckillService iWatchSeckillService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IIndexService iIndexService;

    @PostMapping("/watchSeckill")
    @ResponseBody
    public Result kill(Integer killId, HttpSession session){
        // 获取status
        String status = iWatchSeckillService.getSatus(killId);
        //通过status判断处于什么状态
        if ("0".equals(status)){
            return new Result("300","sorry,秒杀活动尚未开始~");
        }
        if ("2".equals(status)){
            return new Result("301","sorry,秒杀活动已经结束~");
        }else{
            ListOperations lop = redisTemplate.opsForList();

            Object o = lop.leftPop("sk_"+killId);
            // 处于秒杀中但是已经售空
            if (o==null){
                return new Result("302","sorry,商品已售空,您来晚一步~");
            }else{


            }

            User user = (User) session.getAttribute("user");
            SetOperations sets = redisTemplate.opsForSet();
            // 考虑一个人是否重复抢购
            // 如果重复抢购把商品信息返回redis
            boolean flag = sets.isMember("success_"+killId,user.getUserId());
            if(flag){
                //说明这个人已经抢购过了
                //因为之前已经删除了,所以还需要将这人非法占用的商品再次放回到redis秒杀集合中去.
                lop.leftPush("sk_"+killId,killId);
                return new Result("303","您的秒杀名额只有1个!不能重复抢购");
            }else{
                //第一次进来...
                sets.add("success_"+killId,user.getUserId());

                //秒杀的信息发送到rabbitmq中
                iWatchSeckillService.save2Rbm(user.getUserId(),killId);

                return new Result("200","恭喜您抢购成功!");
            }
        }
    }

    @GetMapping("/watchSeckillJsp")
    public String watchSeckillJsp(Integer killId, Model model){
        List<Navclass> navClassList = iIndexService.navClassList();
        model.addAttribute("navClassList", navClassList);
        WebSeckill webSeckill = iWatchSeckillService.getSeckillTime(killId);

        model.addAttribute("seckillTime",webSeckill.getStartTime().getTime());
        model.addAttribute("lastTime", webSeckill.getEndTime().getTime() - new Date().getTime());
        return "/mi/seckill/watchSeckill";
    }
}
